package com.gis3c.sys.web.controller;

import com.gis3c.sys.entry.po.Token;
import com.gis3c.sys.entry.vo.TokenUser;
import com.gis3c.sys.entry.vo.UserIdModel;
import com.gis3c.sys.entry.vo.UserModel;
import com.gis3c.sys.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hukekuan on 2017/2/7.
 */
@RestController
@RequestMapping(value = "/api/sys")
public class SysController {
    @Autowired
    private UserService userService;

    /**
     * app登录
     * @param
     * @return 成功则返回token
     */
    @RequestMapping(value = "/tokenlogin",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> tokenLogin(@RequestBody TokenUser user){
        Map<String, String> result = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();
        String userName = user.getUsername(),tokenStr;
        UsernamePasswordToken token = new UsernamePasswordToken(userName, user.getPassword());
        try {
            subject.login(token);
            tokenStr = userService.getTokenByUserName(userName);
            if(tokenStr != null && !"".equals(tokenStr)){
                userService.updateTokeTime(tokenStr);
            }else{
                Token resultToken = userService.createToken(userName);
                userService.updateToken(resultToken);
                tokenStr = resultToken.getToken();
            }
            result.put("token",tokenStr);
        } catch (AuthenticationException e) {
            result.put("token","");
            return result;
        }
        return result;
    }

    @RequestMapping(value = "/user/userlist",method = RequestMethod.GET)
    @ResponseBody
    public Object userListByPage(
            @RequestParam(value = "pagesize", required=true) int pagesize,
            @RequestParam(value = "currentpage", required=true) int currentpage){
        Map<String,Object> result = new HashMap<>();
        result.put("userCount",userService.allUsersCount());
        result.put("userList",userService.allUsersByPage(pagesize,currentpage));
        return result;
    }

    @RequestMapping(value = "/user/usercreate",method = RequestMethod.POST)
    @ResponseBody
    public Object userCreate(@ModelAttribute("user") UserModel user){
        Map<String,Object> result = new HashMap<>();
        if(user != null){
            userService.createUser(user);
            result.put("status","OK");
            return result;
        }
        result.put("status","error");
        return result;
    }

    @RequestMapping(value = "/user/userremove",method = RequestMethod.POST)
    @ResponseBody
    public Object userRemove(@ModelAttribute("userIds") UserIdModel userIds){
        Map<String,Object> result = new HashMap<>();

        if(userIds != null && userIds.getUserids() != null && userIds.getUserids().length > 0){
            userService.deleteUsers(userIds.getUserids());
            result.put("status","OK");
            return result;
        }
        result.put("status","error");
        return result;
    }
}
